package com.ruoyi.project.system.income.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.income.vo.IncomeVo;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.income.mapper.IncomeMapper;
import com.ruoyi.project.system.income.domain.Income;
import com.ruoyi.project.system.income.service.IIncomeService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-11-25
 */
@Service
public class IncomeServiceImpl implements IIncomeService 
{

    private static final Logger log = LoggerFactory.getLogger(IncomeServiceImpl.class);

    @Autowired
    private IncomeMapper incomeMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public Income selectIncomeById(Long id)
    {
        return incomeMapper.selectIncomeById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param income 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Income> selectIncomeList(Income income)
    {
        return incomeMapper.selectIncomeList(income);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param income 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertIncome(Income income)
    {
        return incomeMapper.insertIncome(income);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param income 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateIncome(Income income)
    {
        return incomeMapper.updateIncome(income);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteIncomeByIds(String ids)
    {
        return incomeMapper.deleteIncomeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteIncomeById(Long id)
    {
        return incomeMapper.deleteIncomeById(id);
    }

    @Override
    public String importIncome(List<Income> incomeList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(incomeList) || incomeList.size() == 0)
        {
            throw new BusinessException("导入收入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Income income :incomeList)
        {
            try
            {
                // 验证是否存在这个用户
               Income come = incomeMapper.selSbh(income.getSbh(),income.getSkssnd());
                if (StringUtils.isNull(come))
                {

                    this.insertIncome(income);
                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、账号 " + income.getSbh() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    incomeMapper.updateByIncome(income);
                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、账号 " + income.getSbh() + " 更新成功");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + income.getSbh() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }

    @Override
    public int statisticalByIds(String ids) {
        String [] strings =ids.split(",");
        for (int i=0 ;i<strings.length;i++){
            Income income = incomeMapper.selectIncomeById(new Long(strings[i]));
            BigDecimal  sstj = incomeMapper.selsstj(new Long(strings[i]));
            BigDecimal  fsstj = incomeMapper.selfsstj(new Long(strings[i]));
           BigDecimal srtj = sstj.add(fsstj);
           income.setSstj(sstj);
           income.setFsstj(fsstj);
           income.setSrtj(srtj);
           incomeMapper.updateIncome(income);
        }
        return 1;
    }

    @Override
    public IncomeVo sumIncome(@Param("skssnd")String skssnd) {
        return incomeMapper.sumIncome(skssnd);
    }
}
